package com.gitee.zhuyb.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0.0
 * @Description: #类描述
 * @Date: 2021/12/11 14:52
 * @Copyright (C) ZhuYouBin
 */
@RestController
@RequestMapping("/api/redis/template")
public class RedisTemplateController {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/bound/string")
    public void demo07() {
        BoundValueOperations boundOps = redisTemplate.boundValueOps("k1");
    }

    @RequestMapping("/opsForCompareToBound")
    public void demo06() {
        // opsFor方式操作redis
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("k1", "v100");
        Object k1 = ops.get("k1");

        // bound方式操作redis
        // 事先指定需要操作哪个key
        BoundValueOperations boundOps = redisTemplate.boundValueOps("k1");
        boundOps.set("v200");
        Object obj = boundOps.get();
    }

    @RequestMapping("/zset")
    public void demo05() {
        ZSetOperations ops = redisTemplate.opsForZSet();
        // 等价于redis命令:zadd
        Boolean add = ops.add("k1", "v100", 100);
        DefaultTypedTuple<String> dtt1 = new DefaultTypedTuple<>("v100", 100.0);
        DefaultTypedTuple<String> dtt2 = new DefaultTypedTuple<>("v200", 200.0);
        Set<DefaultTypedTuple<String>> set = new HashSet<>();
        set.add(dtt1);
        set.add(dtt2);
        Long k2 = ops.add("k2", set);
        // 等价于redis命令:zrange
        Set k1 = ops.range("k1", 0, -1);
        Set k11 = ops.rangeWithScores("k1", 0, -1);
        // 等价于redis命令:zrangebyscore
        Set k12 = ops.rangeByScore("k1", 10.0, 52.0);
        Set k13 = ops.rangeByScoreWithScores("k1", 10.0, 52.0);
        // 等价于redis命令:zrevrangebyscore
        Set k21 = ops.reverseRangeByScore("k2", 100.0, 30.0);
        Set k22 = ops.reverseRangeByScoreWithScores("k2", 100.0, 30.0);
        // 等价于redis命令:zincrby
        Double aDouble = ops.incrementScore("k1", "v100", 10);
        Double aDouble1 = ops.incrementScore("k1", "v200", -20);
        // 等价于redis命令:zrem
        Long remove = ops.remove("k1", "v100", "v300");
        // 等价于redis命令:zcount
        Long k14 = ops.count("k1", 10.0, 52.0);
        // 等价于redis命令:zrank
        Long rank = ops.rank("k1", "v100");
    }

    @RequestMapping("/hash")
    public void demo04() {
        HashOperations ops = redisTemplate.opsForHash();
        // 等价于redis命令:hset
        ops.put("k1", "h1", "v100");
        // 等价于redis命令:hget
        Object o = ops.get("k1", "h2");
        // 等价于redis命令:hkeys
        Set k11 = ops.keys("k1");
        // 等价于redis命令:hvals
        List k1 = ops.values("k1");
        // 等价于redis命令:hmset
        Map<String, String> map = new HashMap<>();
        map.put("h1", "v100");
        map.put("h2", "v200");
        ops.putAll("k2", map);
        // 等价于redis命令:hdel
        Long delete = ops.delete("k1", "h1", "h3");
        // 等价于redis命令:hsetnx
        Boolean aBoolean = ops.putIfAbsent("k1", "h2", "v200");
        // 等价于redis命令:hincrby
        Long increment = ops.increment("k1", "h1", 10);
        Long increment1 = ops.increment("k1", "h2", -20);
    }

    @RequestMapping("/set")
    public void demo03() {
        SetOperations ops = redisTemplate.opsForSet();
        // 等价于redis命令:sadd
        ops.add("k1", "100", "200", "300");
        // 等价于redis命令:smembers
        Set k1 = ops.members("k1");
        // 等价于redis命令:sismember
        Boolean k11 = ops.isMember("k1", "400");
        // 等价于redis命令:sacrd
        Long k12 = ops.size("k1");
        // 等价于redis命令:srem
        Long k13 = ops.remove("k1", "100", "300");
        // 等价于redis命令:spop
        Object k14 = ops.pop("k1");
        List k15 = ops.pop("k1", 2);
        // 等价于redis命令:srandmember
        Object k16 = ops.randomMember("k1");
        List k17 = ops.randomMembers("k1", 2);
        // 等价于redis命令:smove
        Boolean move = ops.move("k1", "v100", "k2");
        // 等价于redis命令:sinter
        List<String> keyLists = new ArrayList<>();
        keyLists.add("k1");
        keyLists.add("k2");
        Set intersect = ops.intersect(keyLists);
        // 求k1和k2的交集,然后将结果保存到k3中
        Long aLong2 = ops.intersectAndStore("k1", "k2", "k3");
        // 等价于redis命令:sunion
        Set union = ops.union(keyLists);
        // 求k1和k2的并集,然后将结果保存到k3中
        Long aLong1 = ops.unionAndStore("k1", "k2", "k3");
        // 等价于redis命令:sdiff
        Set difference = ops.difference(keyLists);
        // 求k1和k2的差集,然后将结果保存到k3中
        Long aLong = ops.differenceAndStore("k1", "k2", "k3");
    }

    @RequestMapping("/list")
    public void demo02() {
        ListOperations ops = redisTemplate.opsForList();
        // 左边添加元素,等价于redis命令:lpush
        ops.leftPush("k1", "100");
        ops.leftPushAll("k2", "100", "200", "300");
        List<Integer> addList = new ArrayList<>();
        addList.add(100);
        addList.add(200);
        ops.leftPushAll("k3", addList);
        // 右边添加元素,等价于redis命令:rpush
        ops.rightPush("k11", "100");
        ops.rightPushAll("k22", "100", "200", "300");
        List<Integer> addList2 = new ArrayList<>();
        addList2.add(100);
        addList2.add(200);
        ops.rightPushAll("k33", addList2);
        // 等价于redis命令:lpop
        Object k2 = ops.leftPop("k2");
        // 等价于redis命令:rpop
        Object k22 = ops.rightPop("k22");
        // 等价于redis命令:rpoplpush
        Object push = ops.rightPopAndLeftPush("k22", "k2");
        // 等价于redis命令:lrange
        List k21 = ops.range("k2", 0, -1);
        // 等价于redis命令:lindex
        Object k221 = ops.index("k22", 1);
        // 等价于redis命令:llen
        Long k33 = ops.size("k33");
        // 等价于redis命令:lrem
        Long k3 = ops.remove("k3", 1, "100");
        // 等价于redis命令:lset
        ops.set("k33", 2, "new_200");
    }

    @RequestMapping("/string")
    public void demo01() {
        // 操作字符串
        ValueOperations ops = redisTemplate.opsForValue();
        // 设置key-value,等价于redis命令:set
        ops.set("k1", "100");
        // 设置过期时间,等价于redis命令:setex
        ops.set("k2", "200", 2, TimeUnit.SECONDS);
        // key不存在,则设置,等价于redis命令:setnx
        ops.setIfAbsent("k3", "300");
        ops.set("k4", "this is a string.");
        // 追加内容,等价于redis命令:append
        ops.append("k4", "redis string");
        // 获取长度,等价于redis命令:strlen
        Long k4 = ops.size("k4");
        // 自增1,等价于redis命令:incr
        ops.increment("k1");
        // 自增指定步长,等价于redis命令:incrby
        ops.increment("k1", 10);
        // 自减1,等价于redis命令:decr
        ops.decrement("k3");
        // 自减指定步长,等价于redis命令:decrby
        ops.decrement("k3", 20);
        Map<String, String> map = new HashMap<>();
        map.put("kk1", "vv100");
        map.put("kk2", "vv200");
        // 等价于redis命令:mset
        ops.multiSet(map);
        List<String> keys = new ArrayList<>();
        keys.add("kk1");
        keys.add("kk2");
        // 等价于redis命令:mget
        List list = ops.multiGet(keys);
        map = new HashMap<>();
        map.put("kk1", "vv100");
        map.put("kk3", "vv300");
        // 等价于redis命令:msetnx
        Boolean aBoolean = ops.multiSetIfAbsent(map);
        // 等价于redis命令:get
        Object k1 = ops.get("k1");
        // 等价于redis命令:getset
        Object obj = ops.getAndSet("kk1", "bak_vv100");
        // 等价于redis命令:getrange
        String kk2 = ops.get("kk2", 0, 1);
        // 等价于redis命令:del
        Boolean k3 = ops.getOperations().delete("k3");
    }


}
